/*
 * Copyright (c) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "keyboard_service.h"
#include <unistd.h>
#include "file_ex.h"
#include "system_ability_definition.h"
#include "hilog_wrapper.h"
#include "keyboard_errors.h"
#include "keyboard_common.h"
#include "cashdevice_service.h"
#include "ikeyboardadaptor.h"
#include "iadaptor.h"

namespace SwanLink {
namespace  Finance {
using namespace OHOS;
namespace {
auto pms = DelayedSpSingleton<KeyboardService>::GetInstance();
const bool G_REGISTER_RESULT = SystemAbility::MakeAndRegisterAbility(
    pms.GetRefPtr());
}

KeyboardService::KeyboardService()
    : SystemAbility(FINANCE_KEYBOARD_SERVICE_ID, true),
    ready_(false)
{
}

KeyboardService::~KeyboardService() {}

void KeyboardService::OnDump() {
}

void KeyboardService::OnStart()
{
    KEYBOARD_HILOGD(KEYBOARD_SRV, "Enter");
    if (ready_) {
        KEYBOARD_HILOGD(KEYBOARD_SRV, "Service is ready, nothing to do");
        return;
    }
    if (!(Init())) {
        KEYBOARD_HILOGE(KEYBOARD_SRV, "Call init failed");
        return;
    }

    if (!Publish(this)) {
        KEYBOARD_HILOGE(KEYBOARD_SRV, "Register to system ability manager failed");
        return;
    }
    ready_ = true;
}

bool KeyboardService::Init()
{
    KEYBOARD_HILOGD(KEYBOARD_SRV, "Enter");
    OnInit(CASH_DEVICE_KEYBOARD, "Keyboard");
    KEYBOARD_HILOGI(KEYBOARD_SRV, "Success");
    return true;
}

void KeyboardService::OnStop()
{
    KEYBOARD_HILOGW(KEYBOARD_SRV, "Enter");
    OnFree();

    KEYBOARD_HILOGW(KEYBOARD_SRV, "Success");
}

int32_t KeyboardService::Dump(int32_t fd, const std::vector<std::u16string> &args)
{
    return ERR_OK;
}


bool KeyboardService::IsSM4Enabled(int handle)
{
    std::shared_ptr<IAdaptor> adaptor = GetAdaptor(handle);
    if (adaptor == nullptr)
        return false;
    iKeyboard_Adaptor *keyboard = static_cast<iKeyboard_Adaptor*>(adaptor.get());
    return keyboard->IsSM4Enabled();
}

int KeyboardService::GetSN(int handle,char sn[],int &len)
{
    std::shared_ptr<IAdaptor> adaptor = GetAdaptor(handle);
    if (adaptor == nullptr)
        return HANDLE_INVALID;
    iKeyboard_Adaptor *keyboard = static_cast<iKeyboard_Adaptor*>(adaptor.get());
    return keyboard->GetSN(sn, len);
}

int KeyboardService::CalculateMAC(int handle,const KeyInfo& key,char plain[],int len,char enc_data[],int &enc_len)
{
    std::shared_ptr<IAdaptor> adaptor = GetAdaptor(handle);
    if (adaptor == nullptr)
        return HANDLE_INVALID;
    iKeyboard_Adaptor *keyboard = static_cast<iKeyboard_Adaptor*>(adaptor.get());
    return keyboard->CalculateMAC(key, plain, len, enc_data, enc_len);
}

int KeyboardService::EncryptData(int handle,const KeyInfo& key,char plain[],int len,char enc_data[],int &len_enc)
{
    std::shared_ptr<IAdaptor> adaptor = GetAdaptor(handle);
    if (adaptor == nullptr)
        return HANDLE_INVALID;
    iKeyboard_Adaptor *keyboard = static_cast<iKeyboard_Adaptor*>(adaptor.get());
    return keyboard->EncryptData(key, plain, len, enc_data, len_enc);
}

int KeyboardService::SetPINLength(int handle,int minLen,int maxLen)
{
    std::shared_ptr<IAdaptor> adaptor = GetAdaptor(handle);
    if (adaptor == nullptr)
        return HANDLE_INVALID;
    iKeyboard_Adaptor *keyboard = static_cast<iKeyboard_Adaptor*>(adaptor.get());
    return keyboard->SetPINLength(minLen, maxLen);
}

int KeyboardService::SetTextMode(int handle,TextMode mode)
{
    std::shared_ptr<IAdaptor> adaptor = GetAdaptor(handle);
    if (adaptor == nullptr)
        return HANDLE_INVALID;
    iKeyboard_Adaptor *keyboard = static_cast<iKeyboard_Adaptor*>(adaptor.get());
    return keyboard->SetTextMode(mode);
}

void KeyboardService::ClearTextMode(int handle)
{
    std::shared_ptr<IAdaptor> adaptor = GetAdaptor(handle);
    if (adaptor == nullptr)
        return;
    iKeyboard_Adaptor *keyboard = static_cast<iKeyboard_Adaptor*>(adaptor.get());
    return keyboard->ClearTextMode();
}

int KeyboardService::LoadMasterKey(int handle,KeyID masterKeyID,unsigned char *KeyValue)
{
    std::shared_ptr<IAdaptor> adaptor = GetAdaptor(handle);
    if (adaptor == nullptr)
        return HANDLE_INVALID;
    iKeyboard_Adaptor *keyboard = static_cast<iKeyboard_Adaptor*>(adaptor.get());
    return keyboard->LoadMasterKey(masterKeyID, KeyValue);
}

int KeyboardService::LoadUserKey(int handle,KeyID masterKeyID,userKeyType user_Key_Type,unsigned char *KeyValue)
{
    std::shared_ptr<IAdaptor> adaptor = GetAdaptor(handle);
    if (adaptor == nullptr)
        return HANDLE_INVALID;
    iKeyboard_Adaptor *keyboard = static_cast<iKeyboard_Adaptor*>(adaptor.get());
    return keyboard->LoadUserKey(masterKeyID, user_Key_Type, KeyValue);
}

int KeyboardService::UpdateMasterKey(int handle,KeyID masterKeyID,unsigned char *KeyValue)
{
    std::shared_ptr<IAdaptor> adaptor = GetAdaptor(handle);
    if (adaptor == nullptr)
        return HANDLE_INVALID;
    iKeyboard_Adaptor *keyboard = static_cast<iKeyboard_Adaptor*>(adaptor.get());
    return keyboard->UpdateMasterKey(masterKeyID, KeyValue);
}

int KeyboardService::UpdateUserKey(int handle,KeyID masterKeyID,userKeyType user_Key_Type,unsigned char *KeyValue)
{
    std::shared_ptr<IAdaptor> adaptor = GetAdaptor(handle);
    if (adaptor == nullptr)
        return HANDLE_INVALID;
    iKeyboard_Adaptor *keyboard = static_cast<iKeyboard_Adaptor*>(adaptor.get());
    return keyboard->UpdateUserKey(masterKeyID, user_Key_Type, KeyValue);
}

int KeyboardService::DeleteUserKey(int handle,KeyID masterKeyID,userKeyType user_Key_Type)
{
    std::shared_ptr<IAdaptor> adaptor = GetAdaptor(handle);
    if (adaptor == nullptr)
        return HANDLE_INVALID;
    iKeyboard_Adaptor *keyboard =
            static_cast<iKeyboard_Adaptor*>(adaptor.get());
    return keyboard->DeleteUserKey(masterKeyID, user_Key_Type);
}

void KeyboardService::ListenForPin(int handle, PinListener* listener, int timeout)
{
    std::shared_ptr<IAdaptor> adaptor = GetAdaptor(handle);
    if (adaptor == nullptr)
        return;
    iKeyboard_Adaptor *keyboard = static_cast<iKeyboard_Adaptor*>(adaptor.get());
    keyboard->ListenForPin(listener, timeout);
}
} // namespace Finance
} // namespace SwanLink
